package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.generator.IFill;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.EcMonitoring;
import com.funmi.lizip.entity.ProtectionEffect;
import com.funmi.lizip.entity.dto.EcMonitoring.EcMonitoringAddRequest;
import com.funmi.lizip.entity.dto.EcMonitoring.EcMonitoringQueryRequest;
import com.funmi.lizip.entity.dto.EcMonitoring.EcMonitoringUpdateRequest;
import com.funmi.lizip.entity.dto.ProtectionEffect.ProtectionEffectAddRequest;
import com.funmi.lizip.entity.dto.ProtectionEffect.ProtectionEffectQueryRequest;
import com.funmi.lizip.entity.dto.ProtectionEffect.ProtectionEffectUpdateRequest;
import com.funmi.lizip.entity.enums.PathEnum;
import com.funmi.lizip.entity.vo.EcMonitoringVo;
import com.funmi.lizip.entity.vo.ProtectionEffectVo;
import com.funmi.lizip.service.EcMonitoringService;
import com.funmi.lizip.service.ProtectionEffectService;
import com.funmi.lizip.utils.MyJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 */
@Api(tags = "生态保护成效模块")
@RestController
@RequestMapping("/protectionEffect")
@Slf4j
public class ProtectionEffectController {

    @Autowired
    private ProtectionEffectService protectionEffectService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "分页获取 生态保护成效")
    @GetMapping("")
    public R getList(ProtectionEffectQueryRequest request) throws IllegalAccessException {
        long current = 1;
        long size = 10;
        ProtectionEffectQueryRequest personnelQueryRequest = MyJsonUtil.setNullValue(request);
        ProtectionEffect personnelQuery = new ProtectionEffect();
        if (request != null) {
            BeanUtils.copyProperties(personnelQueryRequest, personnelQuery);
            current = request.getPage();
            size = request.getPageSize();
        }

        QueryWrapper<ProtectionEffect> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getTitle())){
            queryWrapper.like("title", request.getTitle());
        }

        Page<ProtectionEffect> personnelPage = protectionEffectService.page(new Page<>(current, size), queryWrapper);

        PageDTO<ProtectionEffectVo> personnelVoPage = new PageDTO<>(personnelPage.getCurrent(), personnelPage.getSize(), personnelPage.getTotal());
        List<ProtectionEffectVo> personnelVoList = personnelPage.getRecords().stream().map(personnel -> {
            ProtectionEffectVo personnelVo = new ProtectionEffectVo();
            BeanUtils.copyProperties(personnel, personnelVo);
            if (!ObjectUtils.isEmpty(personnel.getFile())){
                personnelVo.setFile("ProtectionEffect" + "\\" + personnel.getFile());
            }
            return personnelVo;
        }).collect(Collectors.toList());
        personnelVoPage.setRecords(personnelVoList);
        return R.success().data(personnelVoPage);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除 生态保护成效内容")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable int id) {
        boolean b = protectionEffectService.removeById(id);
        if (b) {
            return R.success().message("删除成功");
        }
        return R.success().message("删除失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增 生态保护成效内容")
    @PostMapping("")
    public R add(@RequestBody ProtectionEffectAddRequest request) throws IOException {
        if (request == null) {
            return R.success().data("数据为空");
        }
        ProtectionEffect protectionEffect = new ProtectionEffect();
        BeanUtils.copyProperties(request, protectionEffect);

        if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getFile())) {
            LocalDateTime now = LocalDateTime.now();
            String s = "" + now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth();
            File dest = new File(PathEnum.PATH.getText() + "\\" + "ProtectionEffect" + "\\" + s + "\\" + request.getFile());
            // 将暂存的图片移动到指定文件夹中
            // 文件夹名称
            Path directories = Files.createDirectories(dest.getParentFile().toPath());
            File startFile = new File(PathEnum.PATH.getText() + "\\" + request.getFile());

            boolean b = startFile.renameTo(dest);
            if (!b) {
                throw new ApplicationException("图片保存失败");
            }
            protectionEffect.setFile(s + "\\" + request.getFile());
        }



        boolean save = protectionEffectService.save(protectionEffect);
        if (save) {
                return R.success().message("新增成功");
        }
        return R.success().message("新增失败");
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据id 获取 生态保护成效内容")
    @GetMapping("/{id}")
    public R get(@PathVariable int id) {
        ProtectionEffect protectionEffect = protectionEffectService.getById(id);
        if (ObjectUtils.isEmpty(protectionEffect)){
            return R.success().message("无数据或被删除");
        }
        if(!ObjectUtils.isEmpty(protectionEffect.getFile())){
            protectionEffect.setFile("ProtectionEffect" + "\\"+protectionEffect.getFile());
        }
        return R.success().data(protectionEffect);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改 生态保护成效内容")
    @PutMapping("")
    public R update(@RequestBody ProtectionEffectUpdateRequest request) {
        ProtectionEffect protectionEffect = new ProtectionEffect();
        BeanUtils.copyProperties(request, protectionEffect);
        boolean b = protectionEffectService.updateById(protectionEffect);
        if (b) {
            return R.success().message("修改成功");
        }
        return R.success().message("修改失败");
    }

}
